//connection to host and port
var socket = io();
/*
//when user login or logout,system notice
socket.on('loginInfo',function(msg){
	addMsgFromSys(msg);
  Messenger().post({
    message: msg,
    showCloseButton: true
  });
});

//add user in ui
socket.on('userList',function(userList){
	//modify user count
	//modifyUserCount(userList.length);
  addUser(userList);
});

//client review user information after login
socket.on('userInfo',function(userObj){
  //should be use cookie or session
	userSelf = userObj;
  $('#spanuser').text('欢迎您！ '+userObj.name);
});

//review message from toAll
socket.on('toAll',function(msgObj){
  addMsgFromUser(msgObj,false);
});

//review message from toOne
socket.on('toOne',function(msgObj){
  Messenger().post({
    message: "<a href=\"javascript:showSetMsgToOne(\'"+msgObj.from.name+"\',\'"+msgObj.from.id+"\');\">"+msgObj.from.name + " send to you a message:"+ msgObj.msg+"</a>",
    showCloseButton: true
  });
});

socket.on('sendImageToALL',function(msgObj){
  addImgFromUser(msgObj,false);
});
*/
//用户添加
socket.on('userList',function(userList){
  addUser(userList);
});
//用户成功上线
socket.on('userInfo',function(userObj){
  //当前用户信息存在一个全局变量里
  userSelf=userObj
  $('#spanuser').text('欢迎您！ '+userObj.name);
});
//其它用户收到当前用户的上线提醒
socket.on('loginInfo',function(msg){
  addMsgFromSys(msg);
  Messenger().post({
    message: msg,
    showCloseButton: true
  });
});
//接收单发消息
socket.on('toOne',(msgObj)=>{
  Messenger().post({
    message: "<a href=\"javascript:showSetMsgToOne(\'"+msgObj.from.name+"\',\'"+msgObj.from.id+"\');\">"+msgObj.from.name + "对你说:"+ msgObj.msg+"</a>",
    showCloseButton: true
  });
})
//接收消息
socket.on('toAll',(msgObj)=>{
	addMsgFromUser(msgObj,false)
})
//接收图片消息
socket.on('sendImageToALL',function(msgObj){
  	addImgFromUser(msgObj,false);
});
function addUser(userList){
	var parentUl = $('.user-content').children('ul');
	var cloneLi = parentUl.children('li:first').clone();
	parentUl.html('');
	parentUl.append(cloneLi);
	for(var i in userList){
		var cloneLi = parentUl.children('li:first').clone();
		cloneLi.children('a').attr('href',"javascript:showSetMsgToOne('"+userList[i].name+"','"+userList[i].id+"');");
		cloneLi.children('a').children('img').attr('src',userList[i].img);
		cloneLi.children('a').children('span').text(userList[i].name);
		cloneLi.show();
		parentUl.append(cloneLi);
	}
}
function addMsgFromSys(msg){
	$.scojs_message(msg, $.scojs_message.TYPE_OK);
}